﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.Client;
using System.Configuration;

namespace GTA.ResourceAccess
{
    public class TeamSystemProviderDesktop : ITeamSystemProvider
    {
        #region Local Objects
        private static WorkItemStore WIStore = null;
        #endregion

        #region Properties
        protected WorkItemStore WorkItemStore { get { return WIStore; } }
        #endregion

        #region Inicialization
        public WorkItemStore GetWorkItemStore()
        {
            if (WIStore == null)
            {
                using (TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer((ConfigurationManager.AppSettings["TeamSystemServerDestination"])))
                {
                    WIStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
                }
            }

            return WIStore;
        }
        public IGroupSecurityService GetGroupSecurityService()
        {
            IGroupSecurityService groupSecurity = null;
            using (TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer((ConfigurationManager.AppSettings["TeamSystemServerDestination"])))
            {
                groupSecurity = (IGroupSecurityService)tfs.GetService(typeof(IGroupSecurityService));
            }
            return groupSecurity;
        }
        #endregion

        #region Execution Methods
        public WorkItemCollection ExecuteQuery(string wiql)
        {
            return GetWorkItemStore().Query(wiql);
        }
        public void StartTeamSystemInstance()
        {
            GetWorkItemStore();
        }
        #endregion
    }
}
